﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.OMM.Translator.Interfaces;

namespace Sinacor.OMM.Translator.FactoryLogic
{
    public class STMFactoryLogic : IFactoryLogic
    {
        private static IFactoryLogic _instance = null;

        private STMFactoryLogic()
        {
        }

        public static IFactoryLogic GetInstance()
        {
            if (_instance == null)
                _instance = new STMFactoryLogic();

            return _instance;
        }

        #region IFactoryLogic Members

        public ITranslator GetTranslator(string message)
        {
            return GetMessageTranslator(null, message);
        }        
 
        public ITranslator GetTranslator(string label, string message)
        {
            return GetMessageTranslator(label, message);
        }

        private ITranslator GetMessageTranslator(string label, string message)
        {
            ITranslator translator = null;
            string identificator;

            if (message == null)
                throw new ArgumentNullException("message");

            // if the message starts with a "<" character the source is from CBLC, otherwise MegaBolsa.
            if (message.Substring(0, 1) == "<")
            {
                identificator = message.Substring(LayoutCatalog.STM.CblcMessage.ID.StartIndex, LayoutCatalog.STM.CblcMessage.ID.Length);
            }
            else
            {
                identificator = message.Substring(LayoutCatalog.STM.MegaBolsaMessage.ID.StartIndex, LayoutCatalog.STM.MegaBolsaMessage.ID.Length);
            }

            switch (identificator.Trim())
            {
                case "0100":
                    translator = OMM.Translator.Translators.STM.Message0100.GetInstance();
                    break;
                case "0103":
                    translator = OMM.Translator.Translators.STM.Message0103.GetInstance();
                    break;
                case "0105":
                    translator = OMM.Translator.Translators.STM.Message0105.GetInstance();
                    break;
                case "0138":
                    translator = OMM.Translator.Translators.STM.Message0138.GetInstance();
                    break;
                case "0172":
                    translator = OMM.Translator.Translators.STM.Message0172.GetInstance();
                    break;
                case "0411":
                    translator = OMM.Translator.Translators.STM.Message0411.GetInstance();
                    break;
                case "0412":
                    translator = OMM.Translator.Translators.STM.Message0412.GetInstance();
                    break;
                case "0413":
                    translator = OMM.Translator.Translators.STM.Message0413.GetInstance();
                    break;
                case "0414":
                    translator = OMM.Translator.Translators.STM.Message0414.GetInstance();
                    break;
                case "0415":
                    translator = OMM.Translator.Translators.STM.Message0415.GetInstance();
                    break;
                case "0417":
                    translator = OMM.Translator.Translators.STM.Message0417.GetInstance();
                    break;
                case "AN":
                    translator = OMM.Translator.Translators.STM.MessageAN.GetInstance();
                    break;
                case "ANF":
                    translator = OMM.Translator.Translators.STM.MessageANF.GetInstance();
                    break;
                case "AN53":
                    translator = OMM.Translator.Translators.STM.MessageAN53.GetInstance();
                    break;
                default:
                    translator = OMM.Translator.Translators.STM.GenericMessage.GetInstance();
                    break;
            }

            if(translator != null)
                translator.OriginalMessage = new Sinacor.OMM.MessageEntity.OriginalMessage() { Body = message, Label = label };

            return translator;
        }

        #endregion
    }
}
